package cn.itcast.bos.web.action.base;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;

import com.alibaba.fastjson.JSON;
import com.opensymphony.xwork2.ModelDriven;

import cn.itcast.bos.domain.base.Courier;
import cn.itcast.bos.domain.base.Standard;
import cn.itcast.bos.service.base.IcourierService;

@Controller
@Namespace("/")
@Scope("prototype")
@ParentPackage("json-default")
public class CourierAction implements ModelDriven<Courier> {

	private Courier courier = new Courier();

	@Override
	public Courier getModel() {
		// TODO Auto-generated method stub
		return courier;
	}

	@Autowired
	private IcourierService IcourierService;

	private int page;

	private int rows;

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getRow() {
		return rows;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}

	@Action(value = "courier_save", results = {
			@Result(name = "success", location = "pages/base/courier.html", type = "redirect") })
	public String courier_Save() {
		IcourierService.save(courier);
		return "success";
	}

	@Action(value = "courier_pageQuery"/*, results = { @Result(name = "success", type = "json") }*/)
	public void courier_pageQuery() {

		//jpa的一个分页的接口 用于
		Pageable pageable = new PageRequest(page - 1, rows);
		
		
		
		

		//条件查询 对象 
		Specification<Courier> specification=new Specification<Courier>() {
			
			@Override
			                           //  
			public Predicate toPredicate(Root<Courier> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				// TODO Auto-generated method stub
				List<Predicate>list=new ArrayList<Predicate>();
				if(StringUtils.isNotBlank(courier.getCourierNum())){
					Predicate equal1= cb.equal(root.get("courierNum").as(String.class),courier.getCourierNum());
					list.add(equal1);
					
				}
				if(StringUtils.isNotBlank(courier.getCompany())){
					Predicate like1 = cb.like(root.get("company").as(String.class), "%"+courier.getCompany()+"%");
					list.add(like1);
				}
				if(StringUtils.isNotBlank(courier.getType())){
					Predicate like2 = cb.like(root.get("tpye").as(String.class),"%"+courier.getType()+"%" );
					list.add(like2);
				}
				//join 关联查询 standard 就是关联对象
				Join<Courier, Standard> join = root.join("standard");
				if(courier.getStandard()!=null&&StringUtils.isNotBlank(courier.getStandard().getName())){
					Predicate like3 = cb.like(join.get("name").as(String.class),"%"+courier.getStandard().getName()+"%");
					list.add(like3);
				}
				
				Predicate[] array = list.toArray(new Predicate[0] );
				return cb.and(array);
				
			}
			
		};
		Page<Courier> pages= IcourierService.findAll(specification,pageable);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("total", pages.getTotalElements());
		map.put("rows", pages.getContent());
	/*	ServletActionContext.getContext().getValueStack().push(map);*/
		String jsonString = JSON.toJSONString(map);
		try {
			ServletActionContext.getResponse().setCharacterEncoding("utf-8");
			ServletActionContext.getResponse().getWriter().write(jsonString);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		/*return "success";*/
	}

}
